package com.example.mqdemo.Work.fairDispatch.config;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/*
 *@description: 工作队列消息公平分发工厂配置
 *@author: hisen
 *@Time: 2023/11/21  10:42
 */
@Configuration
public class FairWorkConfiguration {
    @Bean(value = "fairWorkFactory")
    public SimpleRabbitListenerContainerFactory workFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        //设置预获取的消息数量
        factory.setPrefetchCount(1);
        // 手动确认消息
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL); // 或者 AcknowledgeMode.MANUAL
        // 设置连接工厂
        factory.setConnectionFactory(connectionFactory);
        return factory;
    }
}